package com.bracketbird.client.gui.club.tournament.rtc;


import com.bracketbird.client.gui.club.tournament.*;
import com.bracketbird.client.model.tournament.*;
import com.bracketbird.clientcore.gui.*;
import com.bracketbird.clientcore.style.*;
import com.bracketbird.clientcore.util.*;

import java.util.*;

/**
 *
 */
public class LevelRankingPanel extends VerticalComponent {

    private TournamentLevel level;
    private SimplePanelComponent rankingHolder = new SimplePanelComponent();
    private int levelCount;


    private TournamentListener<LevelStateEvent> levelStateListner = new TournamentListener<LevelStateEvent>() {
        public void onChange(LevelStateEvent event) {
            if (event.getNewState() instanceof LevelStateEmpty) {
                layoutNoRanking();
            }
            else if (event.getNewState() instanceof LevelStateMatchesLayedOut) {
                bindMathces();
                layoutRanking();
            }
        }
    };

    private TournamentListener<MatchEvent> matchListener = new TournamentListener<MatchEvent>() {
        public void onChange(MatchEvent event) {
            layoutRanking();
        }
    };



    public LevelRankingPanel(TournamentLevel l, int levelCount) {
        super();
        this.levelCount = levelCount;
        this.level = l;
        init();
    }

    private void init() {
        level.addStateListener(levelStateListner);

        add(rankingHolder, new TextLayout(10, 0, 30, 0, null, "100%"));
        initialSetup();
    }

    private void bindMathces() {
        for (Match m : level.getMatches()) {
            m.addMatchChangedListener(matchListener);
        }
    }

    private void initialSetup() {
        bindMathces();
        if (level.isEmpty()) {
            layoutNoRanking();
        }
        else {
            layoutRanking();
        }
    }


    public void layoutNoRanking() {
        VerticalComponent vc = new VerticalComponent();
        vc.add(getHeaderLabel(), RTCLayoutFac2.h1().margin(0, 0, 10, 0));
        vc.add(new HtmlComponent("Matches not layed out yet."), RTCLayoutFac2.h3());
        rankingHolder.add(vc, new TextLayout(null, "100%"));
    }


    public void layoutRanking() {
        if (level instanceof Cup) {
            layoutCupRanking();
        }
        else if (level instanceof Group) {
            layoutGroupRanking();
        }
    }

    private LabelComponent getHeaderLabel() {
        return new LabelComponent("Ranking " + levelCount + ". stage (" + level.getName() + ")");

    }

    private void layoutGroupRanking() {
        VerticalComponent vc = new VerticalComponent();
        vc.add(getHeaderLabel(), RTCLayoutFac2.h1().margin(0, 0, 10, 0));
        StringBuffer sb = new StringBuffer();
        List<AGroup> grs = ((Group) level).getGroups();
        for (AGroup gr : grs) {
            RankingSheet sheet = new RankingSheet(gr.getMatches(), level.getStageSettings());
            GroupScoreSheet gs = new GroupScoreSheet(gr, sheet.getPositions());
            gs.generateHtml(sb);
        }

        vc.add(new HtmlComponent(sb.toString()), new TextLayout(30, 0, 0, 0, null, "100%"));
        rankingHolder.add(vc, new TextLayout(null, "100%"));
    }


    private void layoutCupRanking() {
        VerticalFocusManager vfm = new VerticalFocusManager();
        VerticalComponent panel = new VerticalComponent();
        panel.add(getHeaderLabel(), RTCLayoutFac2.h1().margin(0, 0, 10, 0));
        boolean firstRound = true;
        for (Round round : level.getRounds()) {
            panel.add(new RoundPanel(vfm, round, firstRound), new TextLayout(10, 0, 10, 0, null, "100%"));
            if (firstRound) {
                firstRound = false;
            }
        }
        rankingHolder.add(panel, new TextLayout(null, "100%"));

    }



}
